package com.example.administrator.social.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.social.R;
import com.example.administrator.social.model.ApplyInfo;
import com.example.administrator.social.model.Contact;
import com.example.administrator.social.model.User;
import com.example.administrator.social.util.AllenUtils;
import com.example.administrator.social.util.HttpUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/9/17 0017.
 */

public class SearchContactAdapter extends RecyclerView.Adapter<SearchContactAdapter.ViewHolder> {
    Context mContext;
    List<Contact> mSearchContactList;
    LayoutInflater inflater;
    SearchContactCallback mCallback;
    public SearchContactAdapter(Context context, List<Contact> searchContactList) {
        mContext = context;
        mSearchContactList = searchContactList;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public SearchContactAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(inflater.inflate(R.layout.new_friends_item_layout,parent,false));
    }

    @Override
    public void onBindViewHolder(SearchContactAdapter.ViewHolder holder, int position) {
        Contact contact = mSearchContactList.get(position);
        holder.bindData(contact,position);
    }

    @Override
    public int getItemCount() {
        return mSearchContactList.size();
    }

    public void updateDataList(List<Contact> searchContactList){
        mSearchContactList = searchContactList;
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView headerImage;
        TextView userName,information,agreeText;
        Button agreeButton;
        public ViewHolder(View itemView) {
            super(itemView);
            headerImage = itemView.findViewById(R.id.new_friend_item_header_image);
            userName = itemView.findViewById(R.id.new_friend_item_user_name);
            information = itemView.findViewById(R.id.new_friend_item_information);
            agreeButton = itemView.findViewById(R.id.new_friend_item_agree_button);
            agreeButton.setClickable(true);
            agreeButton.setEnabled(true);
            agreeText = itemView.findViewById(R.id.new_friend_item_agree_text);
        }

        public void bindData(Contact contact, final int position) {
            if (contact.getHeaderImage()==null||contact.getHeaderImage().equals("")){
                Glide.with(mContext).load(HttpUtils.HTTP_URL+"img/defualt_header_icon.jpeg").into(headerImage);
            }else{
                Glide.with(mContext).load(contact.getHeaderImage()).into(headerImage);
            }

            if (contact.isFriend()){
                if (contact.getRemarks()!=null&&!contact.getRemarks().equals("")){
                    userName.setText(contact.getRemarks());
                }else{
                    if(contact.getNickName()==null||contact.getNickName().equals("")){
                        userName.setText(contact.getName());
                    }else{
                        userName.setText(contact.getNickName());
                    }
                }
            }else {
                if(contact.getNickName()==null||contact.getNickName().equals("")){
                    userName.setText(contact.getName());
                }else{
                    userName.setText(contact.getNickName());
                }
            }

            information.setText(mContext.getResources().getString(R.string.phone_number) + contact.getPhone());

            agreeText.setVisibility(contact.isFriend() ? View.VISIBLE : View.GONE);
            agreeButton.setVisibility(contact.isFriend() ? View.GONE : View.VISIBLE);
            agreeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mCallback.agreeButtonOnClick(view, position);
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.itemOnClick(v,position);
                }
            });
        }
    }

    public void setSearchContactCallback(SearchContactCallback callback){
        mCallback = callback;
    }
    public interface SearchContactCallback{
        void itemOnClick(View view,int position);
        void agreeButtonOnClick(View view,int position);
    }
}
